package com.ysd.lis.service.bac.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.bac.BacObserResult;
import com.ysd.lis.entity.bac.BacObserResultNoGerm;
import com.ysd.lis.entity.lab.LabMaininfo;
import com.ysd.lis.entity.lab.LabTextResult;
import com.ysd.lis.mapper.bac.BacObserResultNoGermMapper;
import com.ysd.lis.service.bac.BacObserResultNoGermService;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wh
 * @since 2025-03-21
 */
@Service
public class BacObserResultNoGermServiceImpl extends ServiceImpl<BacObserResultNoGermMapper, BacObserResultNoGerm> implements BacObserResultNoGermService {

    @Override
    public Result saveOrUpdateResult(List<BacObserResultNoGerm> bacObserResultNoGerms) {
        List<BacObserResultNoGerm> collect = bacObserResultNoGerms.stream().filter(item -> StringUtils.isNotBlank(item.getObserResult())).collect(Collectors.toList());

        boolean b = this.saveOrUpdateBatch(collect);
        if (b) {
            return Result.succ(1, "成功", null);
        } else {
            return Result.fail(400, "失败", "");
        }
    }

    @Override
    public Result batchDelete(List<BacObserResultNoGerm> bacObserResultNoGerms) {
        List<String> ids = bacObserResultNoGerms.stream().map(BacObserResultNoGerm::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }

    @Override
    public Result getObserResultList(BacObserResultNoGerm bacObserResultNoGerms) {
        if (StringUtils.isBlank(bacObserResultNoGerms.getRptGroup())) {
            throw new RuntimeException("报告单元为空");
        }
        if (StringUtils.isBlank(bacObserResultNoGerms.getSampleNo())) {
            throw new RuntimeException("标本号");
        }
        if (BeanUtil.isEmpty(bacObserResultNoGerms.getSampleda())) {
            throw new RuntimeException("检验日期为空");
        }
        MPJLambdaWrapper<BacObserResultNoGerm> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(BacObserResultNoGerm.class);
        queryWrapper.selectAs("test", SysUser::getRealname, BacObserResultNoGerm::getUserName);
        queryWrapper.leftJoin(SysUser.class, "test", p -> p.eq(SysUser::getId, BacObserResultNoGerm::getCreator).eq(SysUser::getDelFlag, 0));
        queryWrapper.eq(BacObserResultNoGerm::getDelFlag, 0);

        queryWrapper.eq(BacObserResultNoGerm::getRptGroup, bacObserResultNoGerms.getRptGroup());
        queryWrapper.eq(BacObserResultNoGerm::getSampleNo, bacObserResultNoGerms.getSampleNo());
        Timestamp timestamp = new Timestamp(bacObserResultNoGerms.getSampleda().getTime());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = sdf.format(timestamp);
        Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
        Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
        queryWrapper.ge(BacObserResultNoGerm::getSampleda, startTime);
        queryWrapper.le(BacObserResultNoGerm::getSampleda, endTime);
        queryWrapper.eq(StringUtils.isNotBlank(bacObserResultNoGerms.getTestFlowNo()), BacObserResultNoGerm::getTestFlowNo, bacObserResultNoGerms.getTestFlowNo());
        queryWrapper.eq(StringUtils.isNotBlank(bacObserResultNoGerms.getTestNodeNo()), BacObserResultNoGerm::getTestNodeNo, bacObserResultNoGerms.getTestNodeNo());
        List<BacObserResultNoGerm> list = this.list(queryWrapper);
        return Result.succ(1, "请求成功", list);
    }
}
